#!/usr/bin/env python

import sys, urllib
from liwe.module import LiweModule
from HTMLParser import HTMLParser

class SiteUtils ( LiweModule ):
	def __init__ ( self, liwe ):
		super ( SiteUtils, self ).__init__ ( liwe, "site_utils" )

		self.dbm = self.liwe.dbm

	def clean_html ( self, txt ):

		opts = {
    			"output-xhtml": 1,     # XHTML instead of HTML4
    			"indent": 1,           # Pretty; not too much of a performance hit
    			"tidy-mark": 0,        # No tidy meta tag in output
    			"wrap": 0,             # No wrapping
    			"alt-text": "",        # Help ensure validation
    			"force-output": 1,     # May not get what you expect but you will get something
			"word-2000" : 1,       # Filter Word tags
			"bare" : 1,
		        "clean" : 1,
			"drop-proprietary-attributes" : 1,
			"fix-backslash" : 1,
			"fix-bad-comments" : 1,
			"logical-emphasis" : 1,
			"output-html" : 1,
			"preserve-entities" : 1,
			"merge-divs" : 0,
			"merge-spans" :0, 
			#"quote-ampersand" : 1,
			#"quote-marks" : 1,
			#"join-styles" : 0,
			"show-body-only" : 0
    			} 
			
		try:
			from tidylib import tidy_document
			#sys.stderr.write ( "\n\n\n**** tidy_document: opts = %s\n" % opts ) 
			conv_txt, errors =  tidy_document ( txt, opts ) 

		except ImportError as e:
			sys.stderr.write ( "\n\n\n**** WARNING: pytidylib not installed %s ****\n\n\n" %  e )
			try:
				from tidy import lib
				opts [ "show-body-only" ] = 0
				out = lib.parseString ( txt, ** opts ) 
				conv_txt, errors = str ( out ), str ( out.errors )
			except ImportError as e:
				sys.stderr.write ( "\n\n\n**** WARNING: tidy not installed %s ****\n\n\n" %  e )
				return { "clean_html" : txt, "errors" : "You do not have pytidylib or tidy installed" }

		return { "clean_html" : conv_txt, "errors" : { 'errors' : errors } }	

